﻿using System;
using System.Data;
using YouthCMS.Data;

namespace YouthCMS
{
    public partial class Teachers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Username"] == null)
            {
                Response.Redirect("Default.aspx?login=false");
            }
            CreateZipcodeList();
        }

        public void SubmitTeacher(object sender, EventArgs e)
        {
            var teacherHandler = new TeacherHandler();
            var createdTeacher = false;

            createdTeacher = teacherHandler.CreateTeacher(txtBoxUsername.Text, txtBoxTeacherPassword.Text, txtBoxTeacherSSN.Text, txtBoxTeacherFirstName.Text, txtBoxTeacherLastName.Text,
                 txtBoxAddress1.Text, txtBoxAddress2.Text, int.Parse(txtBoxPhone1.Text), int.Parse(txtBoxMobile.Text), txtBoxEmail.Text,
                 int.Parse(ddLZipcode.SelectedValue), txtBoxInitials.Text, int.Parse(txtBoxWorkPhone.Text),
                 byte.Parse(chbAddressProtection.Text), byte.Parse(chbPhoneProtection.Text));
            if (createdTeacher == true)
            {
                Response.Redirect("~/Default.aspx?created=true");
            }
        }

        public DataSet MoveList()
        {
            var dataBase = new Database();
            var adapter = dataBase.findZipcode();
            var ds = new DataSet("MyDataSet");
            adapter.Fill(ds, "Zipcode");
            return ds;
        }

        public void CreateZipcodeList()
        {
            DataSet ds = MoveList();
            DataTable dt = ds.Tables["Zipcode"];

            ddLZipcode.DataSource = dt;
            ddLZipcode.DataTextField = "Zipcode";


            ddLZipcode.DataBind();

        }



        public void GeneratePassword(object sender, EventArgs e)
        {
            const int passwordLength = 10;
            const string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
            var chars = new char[passwordLength];
            var rd = new Random();

            for (var i = 0; i < passwordLength; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }


            txtBoxTeacherPassword.Text = new string(chars);
        }
    }
}